<template>
    <div>
     <van-form @submit="onSubmit">
  <van-cell-group inset>
    <van-field
      v-model="userInfo.username"
      name="username"
      label="用户名"
      placeholder="用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
      v-model="userInfo.psw"
      type="password"
      name="pwd"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
  </van-cell-group>
  <div style="margin: 16px;">
    <van-button round block type="primary" native-type="submit">
      提交
    </van-button>
  </div>
</van-form>
<van-button @click="$router.push('/register')">去注册页</van-button>
    </div>
</template>

<script>
import { Toast } from 'vant'
import { onLogin } from '_api'
export default {
  data () {
    return {
      userInfo: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      onLogin({
        ...params,
        deviceId: '111',
        deviceName: 'aaa'
      }).then(res => {
        if (res.data.code === 0) {
          localStorage.setItem('token', res.data.data.token)
          Toast({
            type: 'success',
            message: '登录成功',
            onClose: () => {
              if (this.$route.query.from) {
                this.$router.replace(this.$route.query.from)
              } else {
                this.$router.push('/home')
              }
            }
          })
        } else {
          Toast.fail('登录失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
